package com.emerquim.persistence.dao;

// default package


import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * Evento entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .Evento
 * @author MyEclipse Persistence Tools
 */

public class EventoDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(EventoDAO.class);
	// property constants
	public static final String EVE_DESC = "eveDesc";
	public static final String EVE_TELS = "eveTels";
	public static final String EVE_FAX = "eveFax";
	public static final String EVE_DIR = "eveDir";
	public static final String EVE_ZONA = "eveZona";
	public static final String EVE_SITIO_DESC = "eveSitioDesc";
	public static final String EVE_ESTADO = "eveEstado";

	public void save(Evento transientInstance) {
		log.debug("saving Evento instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Evento persistentInstance) {
		log.debug("deleting Evento instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void update(Evento persistentInstance) {
		log.debug("updating Evento instance");
		try {
			getSession().update(persistentInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public Evento findById(long id) {
		log.debug("getting Evento instance with id: " + id);
		try {
			Evento instance = (Evento) getSession().get("com.emerquim.persistence.dao.Evento", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findEventoFechasTipo(Date fechaInicial, Date fechaFinal, String tipo) {
		log.debug("Encontrar eventos entre las fechas: " + fechaInicial + " y " + fechaFinal + " . TipoEvento: " + tipo);
		try {
			String tipoEvento = "";
			if(!tipo.equals("T"))
				tipoEvento = " and model.eveEstado = ?";			
			String queryString = "from Evento as model where model.eveFechaEve >= ? and  model.eveFechaEve <= ?" + tipoEvento;
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, fechaInicial);
			queryObject.setParameter(1, fechaFinal);
			if(!tipo.equals("T"))
				queryObject.setParameter(2, tipo);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByExample(Evento instance) {
		log.debug("finding Evento instance by example");
		try {
			List results = getSession().createCriteria("Evento").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Evento instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Evento as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	/**
	 * Metodo para retornar la lista de eventos dado el nombre del reportante
	 * @param nombreReportante Nombre del reportante
	 * @param idTipoReportante El tipo del reportante
	 * @return
	 */
	public List encontrarReportantes(String nombreReportante, String idTipoReportante) {
		
		try {
			String queryString = "from Evento as model where model.eveReportante like '%"+nombreReportante+"%'" +
					" and model.tipoReporta = " + idTipoReportante;
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByEveDesc(Object eveDesc) {
		return findByProperty(EVE_DESC, eveDesc);
	}

	public List findByEveTels(Object eveTels) {
		return findByProperty(EVE_TELS, eveTels);
	}

	public List findByEveFax(Object eveFax) {
		return findByProperty(EVE_FAX, eveFax);
	}

	public List findByEveDir(Object eveDir) {
		return findByProperty(EVE_DIR, eveDir);
	}

	public List findByEveZona(Object eveZona) {
		return findByProperty(EVE_ZONA, eveZona);
	}

	public List findByEveSitioDesc(Object eveSitioDesc) {
		return findByProperty(EVE_SITIO_DESC, eveSitioDesc);
	}

	public List findByEveEstado(Object eveEstado) {
		return findByProperty(EVE_ESTADO, eveEstado);
	}

	public List findAll() {
		log.debug("finding all Evento instances");
		try {
			String queryString = "from Evento";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Evento merge(Evento detachedInstance) {
		log.debug("merging Evento instance");
		try {
			Evento result = (Evento) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Evento instance) {
		log.debug("attaching dirty Evento instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Evento instance) {
		log.debug("attaching clean Evento instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}